package com.cloudkinto.service.finance.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.common.DicBusinessItemConstant;
import com.cloudkinto.common.constant.SysConstant;
import com.cloudkinto.common.exception.BizExceptionI18;
import com.cloudkinto.common.utils.TimeUtils;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.dao.*;
import com.cloudkinto.entity.*;
import com.cloudkinto.extentity.InvoiceRecordChechkDto;
import com.cloudkinto.service.dic.vo.LanguageValueEntity;
import com.cloudkinto.service.email.MailSenderService;
import com.cloudkinto.service.file.FileService;
import com.cloudkinto.service.finance.RechargeRecordService;
import com.cloudkinto.service.finance.vo.*;
import com.cloudkinto.service.invoice.InvoiceRecordService;
import com.cloudkinto.service.invoice.InvoiceTitleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**
 * <p>
 * service实现类
 * </p>
 *
 * @author ding
 * @since 2020-06-10
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class RechargeRecordServiceImpl extends ServiceImpl<RechargeRecordDao, RechargeRecordDo> implements RechargeRecordService {
    private static final String MONEY_TYPE_RMB = "0";
    private static final String MONEY_TYPE_JP = "1";
    private static final String MONEY_TYPE_US = "2";
    @Autowired
    private RechargeRecordDao dao;
    @Autowired
    private RechargeTypeDao rechargeTypeDao;
    @Autowired
    private CompanyAccountDao accountDao;
    @Autowired
    private ExchangeRateDao exchangeRateDao;
    @Autowired
    private FileService fileService;
    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    @Autowired
    private MailSenderService mailSenderService;
    @Autowired
    private CompanyDao companyDao;
    @Autowired
    private InvoiceRecordService invoiceRecordService;
    @Autowired
    private InvoiceRecordDao invoiceRecordDao;
    @Autowired
    private InvoiceTitleService invoiceTitleService;

    @Value("${gbc.scheduling}")
    private String type;

    @Override
    public SingleResult addInit(Long companyId) {
        LambdaQueryWrapper<CompanyAccountDo> wrapper = new LambdaQueryWrapper();
        wrapper.eq(CompanyAccountDo::getCompanyId, companyId);
        CompanyAccountDo companyAccountDo = accountDao.selectOne(wrapper);
        Map<String, Double> amount = new HashMap<>();
        if (companyAccountDo == null) {
            amount.put("balanceJpy", 0d);
            amount.put("balanceRmb", 0d);
        } else {
            ExchangeRateDo exchangeRate = exchangeRateDao.getJPRate();
            amount.put("balanceRmb", new BigDecimal(companyAccountDo.getCompanyAccount() / 100 * exchangeRate.getRate()).setScale(0, RoundingMode.HALF_UP).doubleValue());
            amount.put("balanceJpy", companyAccountDo.getCompanyAccount());
        }
        List<RechargeTypeDo> rechargeTypeDoList = rechargeTypeDao.selectList(new QueryWrapper<>());
        ExchangeRateDo exchangeRate = getRate();
        Map addInit = new HashMap();
        addInit.put("accountAmount", amount);
        if (exchangeRate != null) addInit.put("rate", exchangeRate.getRate());
        addInit.put("payType", rechargeTypeDoList);
        List<IdAndNameEntity> idAndNameEntities = new ArrayList<>();

        idAndNameEntities.add(new IdAndNameEntity(MONEY_TYPE_RMB, "人民币账户"));

        idAndNameEntities.add(new IdAndNameEntity(MONEY_TYPE_JP, "日币账户"));

        addInit.put("moneyType", idAndNameEntities);
        return SingleResult.success(addInit);
    }

    private  ExchangeRateDo getRate() {
        ExchangeRateDo exchangeRate = exchangeRateDao.getJPRate();
        return exchangeRate;
    }

//    @Override
//    public Object add(RechargeAddReq rechargeAddReq, Long companyId, Long userId) {
////        SingleResult singleResult = fileService.uploadTempFile(file, userId);
//        RechargeRecordDo entityDo = new RechargeRecordDo();
//        entityDo.setId(System.currentTimeMillis());
//        entityDo.setRechargeTypeId(Long.parseLong(rechargeAddReq.getPayType()));
//
//        ExchangeRateDo exchangeRate = getRate();
//        if (MONEY_TYPE_RMB.equals(rechargeAddReq.getMoneyType())) {//充值为人民币时选择为0
//            //todo 实时汇率
//            double amountJP = rechargeAddReq.getAmount() / exchangeRate.getRate() * 100;
//            entityDo.setAmountJpy(new BigDecimal(amountJP).setScale(2, RoundingMode.HALF_UP).doubleValue());
//            entityDo.setAmountRmb(rechargeAddReq.getAmount());
//        } else {
//            double amountRMB = rechargeAddReq.getAmount() / 100 * exchangeRate.getRate();
//            entityDo.setAmountRmb(new BigDecimal(amountRMB).setScale(2, RoundingMode.HALF_UP).doubleValue());
//            entityDo.setAmountJpy(rechargeAddReq.getAmount());
//        }
//        entityDo.setCompanyId(companyId);
//        entityDo.setCertificateImageFileId(rechargeAddReq.getFileId() == null ? null : Long.parseLong(rechargeAddReq.getFileId()));
//        entityDo.setTips(rechargeAddReq.getTips());
////        BeanUtils.copyProperties(req, entityDo);
//        handlerAddBefore(entityDo, userId);
//        dao.insert(entityDo);
//        return handlerAddAfter(entityDo);
//    }

//    @Override
//    public Object add(RechargeAddReq rechargeAddReq, Long companyId, Long userId) {
//        RechargeTypeDo rechargeTypeDo = rechargeTypeDao.selectById(rechargeAddReq.getPayType());
//        if (rechargeTypeDo == null) {
//            throw new BizExceptionI18(SysConstant.ERROR_CHARGE_TYPE);
//        }
//        if ("3".equals(rechargeAddReq.getPayType()) && rechargeAddReq.getInvoiceTitleId() != null && rechargeAddReq.getInvoiceTitleId() != 0) {
//            throw new BizExceptionI18(SysConstant.NO_DATA);
//        }
//        RechargeRecordDo entityDo = new RechargeRecordDo();
//        entityDo.setId(System.currentTimeMillis());
//        entityDo.setRechargeTypeId(Long.parseLong(rechargeAddReq.getPayType()));
//        entityDo.setRechargeAccount(1L);//指定充值账号
//
//        ExchangeRateDo exchangeRate = getRate();
//        if (MONEY_TYPE_RMB.equals(rechargeTypeDo.getCurrency())) {//人民币
//            double amountJP = rechargeAddReq.getAmount() / exchangeRate.getRate();
//            entityDo.setAmountJpy(new BigDecimal(amountJP).setScale(0, RoundingMode.HALF_UP).doubleValue());
//            entityDo.setAmountRmb(rechargeAddReq.getAmount());
//        } else {
//            double amountRMB = rechargeAddReq.getAmount() * exchangeRate.getRate();
//            entityDo.setAmountRmb(new BigDecimal(amountRMB).setScale(2, RoundingMode.HALF_UP).doubleValue());
//            entityDo.setAmountJpy(rechargeAddReq.getAmount());
//        }
//        entityDo.setCurrency("0".equals(rechargeTypeDo.getCurrency()) ? DicBusinessItemConstant.currency_rmb : DicBusinessItemConstant.currency_jpy);
//        entityDo.setJpyRate(Double.parseDouble(exchangeRate.getRate() + ""));
//        entityDo.setCompanyId(companyId);
//        entityDo.setCertificateImageFileId(rechargeAddReq.getFileId() == null ? null : Long.parseLong(rechargeAddReq.getFileId()));
//        entityDo.setTips(rechargeAddReq.getTips());
//        handlerAddBefore(entityDo, userId);
//        dao.insert(entityDo);
//        if (rechargeAddReq.getInvoiceTitleId() != null && rechargeAddReq.getInvoiceTitleId() != 0 && 3L != rechargeTypeDo.getId()) {
//            List<InvoiceRecordChechkDto> unMatchInvoicerecord = invoiceRecordDao.getUnMatchInvoicerecord(rechargeAddReq.getAmount(), entityDo.getCurrency(), companyId);
////            unMatchInvoicerecord.stream().filter(invoiceRecordDo -> invoiceRecordDo)
//            if (unMatchInvoicerecord != null) {
//                int count = unMatchInvoicerecord.size();
//                if (count > 0) {
//                    throw new BizExceptionI18(SysConstant.Invoice_HadRecord_Add, rechargeAddReq.getAmount());
//                }
//            }
//            Long recordId = invoiceRecordService.addRechargeInvoice(rechargeAddReq.getInvoiceTitleId(), entityDo.getId(), rechargeAddReq.getAmount(), entityDo.getCurrency(), companyId, userId);
//            entityDo.setInvoiceRecordId(recordId);
//        }
//        threadPoolTaskExecutor.execute(() -> {
//            handlerAddAfter(entityDo);
//        });
//        return null;
//    }

    public Object add(RechargeAddReq rechargeAddReq, Long companyId, Long userId) {
        RechargeTypeDo rechargeTypeDo = rechargeTypeDao.selectById(rechargeAddReq.getPayType());
        if (rechargeTypeDo == null) {
            throw new BizExceptionI18(SysConstant.ERROR_CHARGE_TYPE);
        }
        if ("3".equals(rechargeAddReq.getPayType())
                && rechargeAddReq.getInvoiceTitleId() != null
                && rechargeAddReq.getInvoiceTitleId() != 0
                && rechargeAddReq.getIsPayment() == 1) {
            throw new BizExceptionI18(SysConstant.NO_DATA);
        }
        RechargeRecordDo entityDo = new RechargeRecordDo();
        entityDo.setId(System.currentTimeMillis());
        entityDo.setPayDate(TimeUtils.parseDate(rechargeAddReq.getPayDate()));
        entityDo.setRechargeTypeId(Long.parseLong(rechargeAddReq.getPayType()));
        entityDo.setRechargeAccount(1L);//指定充值账号

        ExchangeRateDo exchangeRate = getRate();
        if (MONEY_TYPE_RMB.equals(rechargeTypeDo.getCurrency())) {//人民币
            double amountJP = rechargeAddReq.getAmount() / exchangeRate.getRate();
            entityDo.setAmountJpy(new BigDecimal(amountJP).setScale(0, RoundingMode.HALF_UP).doubleValue());
            entityDo.setAmountRmb(rechargeAddReq.getAmount());
        } else {
            double amountRMB = rechargeAddReq.getAmount() * exchangeRate.getRate();
            entityDo.setAmountRmb(new BigDecimal(amountRMB).setScale(2, RoundingMode.HALF_UP).doubleValue());
            entityDo.setAmountJpy(rechargeAddReq.getAmount());
        }
        entityDo.setCurrency("0".equals(rechargeTypeDo.getCurrency()) ? DicBusinessItemConstant.currency_rmb : DicBusinessItemConstant.currency_jpy);
        entityDo.setJpyRate(Double.parseDouble(exchangeRate.getRate() + ""));
        entityDo.setCompanyId(companyId);
        entityDo.setCertificateImageFileId(rechargeAddReq.getFileId() == null ? null : Long.parseLong(rechargeAddReq.getFileId()));
        entityDo.setTips(rechargeAddReq.getTips());
        handlerAddBefore(entityDo, userId);
        dao.insert(entityDo);

        Long recordId = null;
        if (StringUtils.isNotBlank(rechargeAddReq.getInvoiceRecordId())) {
            recordId = Long.parseLong(rechargeAddReq.getInvoiceRecordId());
        } else if (rechargeAddReq.getInvoiceTitleId() != null && rechargeAddReq.getInvoiceTitleId() != 0
                && 3L != rechargeTypeDo.getId() && rechargeAddReq.getIsPayment() == 1) {
            List<InvoiceRecordChechkDto> unMatchInvoicerecord = invoiceRecordDao.getUnMatchInvoicerecord(rechargeAddReq.getAmount(), entityDo.getCurrency(), companyId);
//            unMatchInvoicerecord.stream().filter(invoiceRecordDo -> invoiceRecordDo)
            if (unMatchInvoicerecord != null) {
                int count = (int) unMatchInvoicerecord.size();
                if (count == 1) {
                    recordId = unMatchInvoicerecord.get(0).getId();
                } else if (count > 1) {
                    dao.deleteById(entityDo.getId());
                    return unMatchInvoicerecord;
                }
            }else {
                recordId = invoiceRecordService.addRechargeInvoice(rechargeAddReq.getInvoiceTitleId(), entityDo.getId(), rechargeAddReq.getAmount(), entityDo.getCurrency(), companyId, userId);
            }
        }
        entityDo.setInvoiceRecordId(recordId);
        InvoiceTitleDo invoiceTitleDo = invoiceTitleService.getById(rechargeAddReq.getInvoiceTitleId());
        if (invoiceTitleDo != null)
            entityDo.setPayAccount(invoiceTitleDo.getInvoiceTitle());
        updateById(entityDo);
        threadPoolTaskExecutor.execute(() -> {
            handlerAddAfter(entityDo);
        });
        return null;
    }

    @Override
    public void addRechargeInvoice(InvoiceRecordPatchReq req, Long companyId, Long userId) {
        RechargeRecordDo entityDo = dao.selectById(req.getRechargeRecordId());
        if (entityDo == null) {
            throw new BizExceptionI18(SysConstant.CHARGE_RECORD_NO_EXIST);
        }
        Long currency = entityDo.getCurrency();
        RechargeTypeDo rechargeTypeDo = rechargeTypeDao.selectById(entityDo.getRechargeTypeId());
        if (rechargeTypeDo == null || !"0".equals(rechargeTypeDo.getCurrency())) {
            throw new BizExceptionI18(SysConstant.ERROR_CHARGE_TYPE);
        }
        if (currency == null) {
            currency = "0".equals(rechargeTypeDo.getCurrency()) ? DicBusinessItemConstant.currency_rmb : DicBusinessItemConstant.currency_jpy;
        }
        if (req.getInvoiceTitleId() != null && req.getInvoiceTitleId() != 0) {
            Long recordId = invoiceRecordService.addRechargeInvoice(req.getInvoiceTitleId(), entityDo.getId(), req.getInvoiceAmount(), currency, companyId, userId);
            entityDo.setInvoiceRecordId(recordId);
            updateById(entityDo);
        } else {
            throw new BizExceptionI18(SysConstant.CHARGE_TITLE_ERROR);
        }
    }

    //做新增之前要执行的业务定制处理
    private void handlerAddBefore(RechargeRecordDo entityDo, Long userId) {
        entityDo.setCreateBy(userId);
        entityDo.setCreateTime(new Date());
        entityDo.setDeleteFlag(SysConstant.DELETE_FLAG_FALSE);
    }

    //将新增的do转换成要返回的实体
    private Object handlerAddAfter(RechargeRecordDo entityDo) {
        //文件操作
        fileService.uploadFile(entityDo.getCertificateImageFileId());
        if (!"dev".equals(type))
            threadPoolTaskExecutor.execute(() -> {
                RechargeTypeDo rechargeTypeDo = rechargeTypeDao.selectById(entityDo.getRechargeTypeId());
                CompanyDo companyDo = companyDao.selectById(entityDo.getCompanyId());
                mailSenderService.sendEmail("您有新的充值申请单需要审核", companyDo.getCompanyCode() + "<br>充值人民币:" + entityDo.getAmountRmb() + "<br>充值方式:" + rechargeTypeDo.getTypeName() + "(" + rechargeTypeDo.getAccountName() + ")" + "<br>请尽快审核<br>备注：<br>" + entityDo.getTips() + "<br>充值凭证<br>" + " <div style=\"width: 100vw;overflow: auto\">\n" + "        <a href=\"https://891.cloudkinto.com/\">\n" + fileService.getFilePath(entityDo.getCertificateImageFileId()) + "        </a>\n" + "    </div>", SysConstant.cloudkintEmail);

            });
        return null;
    }

    @Override
    public SingleResult updateInit(Long id) {
        return SingleResult.success("");
    }

    @Override
    public Object update(Object req, Long userId) {
        /*RechargeRecordDo entityDo = dao.selectById(req.getId());
        BeanUtils.copyProperties(req, entityDo);
        handlerUpdateBefore(entityDo, userId);
        dao.updateById(entityDo);
        return handlerUpdateAfter(entityDo);*/
        return null;
    }

    //编辑校验
    private void updateCheck(Object req) {

    }

    //做修改之前要执行的业务定制处理
    private void handlerUpdateBefore(RechargeRecordDo entityDo, Long userId) {
//        entityDo.setUpdateBy(userId);
//        entityDo.setUpdateTime(new Date());
    }

    //做修改之后要执行的业务定制处理
    private Object handlerUpdateAfter(RechargeRecordDo entityDo) {
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        return res;
    }

    @Override
    public int delete(Long id, Long userId) {
        RechargeRecordDo entityDo = dao.selectById(id);
        handlerDeleteBefore(entityDo, userId);
        return dao.deleteById(id);
    }

    //做删除之前要执行的业务定制处理
    private void handlerDeleteBefore(RechargeRecordDo entityDo, Long userId) {
        entityDo.setDeleteBy(userId);
        entityDo.setDeleteTime(new Date());
    }

    @Override
    public Object detail(Long id) {
        RechargeRecordDo entityDo = dao.selectById(id);
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        return handlerDetailAfter(res);
    }

    //查询明细之后要执行的业务定制处理
    private Object handlerDetailAfter(Object res) {
        return res;
    }

    @Override
    public SingleResult pageInit() {
        List<LanguageValueEntity> mapList = new ArrayList<>();
        //0 待审核  1充值成功 2充值取消
        mapList.add(new LanguageValueEntity(0, "等待审核"));
        mapList.add(new LanguageValueEntity(1, "充值成功"));
        mapList.add(new LanguageValueEntity(2, "充值取消"));
        return SingleResult.success(mapList);
    }

    @Override
    public List<RechargeRes> getList(Map<String, String> map) {
        QueryWrapper<RechargeRecordDo> wrapper = queryBuild(map);
        List<RechargeRecordDo> list = this.list(wrapper);
        List<RechargeRes> result = handlerListAfter(list);
        return result;
    }

    @Override
    public PageResult<RechargeRes> getListPage(Map<String, String> map) {
        QueryWrapper<RechargeRecordDo> wrapper = queryBuild(map);
        IPage<RechargeRecordDo> page = pageInit(map);
        page = this.page(page, wrapper);
        List<RechargeRes> resList = handlerListAfter(page.getRecords());
        PageResult result = new PageResult(resList, (int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), (int) page.getPages());
        return result;
    }

    //查询分页列表之后对列表数据的处理
    private List<RechargeRes> handlerListAfter(List<RechargeRecordDo> list) {
//        Map<String, String> payTypeMap = new HashMap();
//        //0支付宝 1对公转账  对私转账 3 微信 4 系统充值
//        payTypeMap.put("0", "支付宝");
//        payTypeMap.put("1", "对公转账");
//        payTypeMap.put("2", "对私转账");
//        payTypeMap.put("3", "微信");
//        payTypeMap.put("4", "系统充值");

        Map<String, String> statusMap = new HashMap<>();
        //0 待审核  1充值成功 2充值取消
        statusMap.put("0", "等待审核");
        statusMap.put("1", "充值成功");
        statusMap.put("2", "充值取消");
        List<RechargeRes> result = new ArrayList<>();
        for (RechargeRecordDo recordDo : list) {
            RechargeRes recordRes = new RechargeRes();
            BeanUtils.copyProperties(recordDo, recordRes);
            if (recordDo.getCurrency() == null) {
                RechargeTypeDo rechargeTypeDo = rechargeTypeDao.selectById(recordDo.getRechargeTypeId());
                if (rechargeTypeDo != null) {
                    recordRes.setCurrency("0".equals(rechargeTypeDo.getCurrency()) ? DicBusinessItemConstant.currency_rmb : DicBusinessItemConstant.currency_jpy);
                }
            }
            if (recordDo.getRechargeAccount() == null) {
                recordDo.setRechargeAccount(1L);
            }
            recordRes.setRechargeAccount(recordDo.getRechargeAccount() == 0L ? "人民币账户" : "日币账户");
            if (recordDo.getRechargeTypeId() == 0) {//type  0 是筋斗云后台充值
                recordRes.setPayType("后台充值");
            } else {
                RechargeTypeDo rechargeTypeDo = rechargeTypeDao.selectById(recordDo.getRechargeTypeId());
                if (rechargeTypeDo == null) {
                    recordRes.setPayType("后台充值");
                } else {
                    //日本国内账户也可以申请补开
                    if (3L == rechargeTypeDo.getId()) {
                        recordRes.setEnableApply(0);
                    } else {
                        recordRes.setEnableApply(1);
                    }
//                    if ("0".equals(rechargeTypeDo.getCurrency())) {
//                        recordRes.setEnableApply(1);
//                    } else {
//                        recordRes.setEnableApply(0);
//                    }
                    recordRes.setPayType(rechargeTypeDo.getTypeName());
                }
            }
            recordRes.setInvoiceRecordId(recordDo.getInvoiceRecordId());
            recordRes.setAmountJp(recordDo.getAmountJpy());
            recordRes.setAmountRmb(recordDo.getAmountRmb());
//            if (recordDo.getRechargeTypeId() == 0L) {
//                if (recordDo.getCurrency() != null) {
//                    //后台充值
//                    if (DicBusinessItemConstant.currency_rmb.equals(recordDo.getCurrency())) {
//                        //人名币
//                        recordRes.setAmountJp(MathUtils.div(0, recordDo.getAmountRmb(), recordDo.getJpyRate() == null ? 1 : recordDo.getJpyRate()));
//                    } else {
//                        recordRes.setAmountRmb(MathUtils.numberFormat(2, MathUtils.mul(recordDo.getAmountJpy(), recordDo.getJpyRate() == null ? 1 : recordDo.getJpyRate())));
//                    }
//                }
//            }
//            else {
//                RechargeTypeDo rechargeTypeDo = rechargeTypeDao.selectById(recordDo.getRechargeTypeId());
//                if (rechargeTypeDo == null) {
//                    throw new BizException("充值方式不正确");
//                }
//                //如果重置的是人民币账户 ，则把日币按照汇率重新算
//                if ("0".equals(rechargeTypeDo.getCurrency())) { //人民币
//                    recordRes.setAmountJp(MathUtils.div(0, recordDo.getAmountRmb(), recordDo.getJpyRate() == null ? 1 : recordDo.getJpyRate()));
//                } else {
//                    recordRes.setAmountRmb(MathUtils.numberFormat(2, MathUtils.mul(recordDo.getAmountJpy(), recordDo.getJpyRate() == null ? 1 : recordDo.getJpyRate())));
//                }
//            }

            recordRes.setStatus(statusMap.get(recordDo.getStatus()));
//            recordRes.setCertificateImagePath();
            result.add(recordRes);
        }
        return result;
    }

    private IPage<RechargeRecordDo> pageInit(Map<String, String> map) {
        IPage<RechargeRecordDo> page = new Page();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 10);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }

    private QueryWrapper<RechargeRecordDo> queryBuild(Map<String, String> map) {
        QueryWrapper<RechargeRecordDo> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(map.get("startDate"))) {
            wrapper.ge("create_time", map.get("startDate"));
        }
        if (StringUtils.isNotBlank(map.get("endDate"))) {
            wrapper.lt("create_time", TimeUtils.addDaysFormat(map.get("endDate"), "yyyy-MM-dd", 1));
        }
        if (StringUtils.isNotBlank(map.get("type"))) {
            wrapper.eq("status", map.get("type"));
        }
        if (StringUtils.isNotBlank(map.get("companyId"))) {
            wrapper.eq("company_id", map.get("companyId"));
        }
        wrapper.orderByDesc("id");
        return wrapper;
    }


}
